<template>
  <div class="container">
    <el-dialog
      :title="showTitle"
      :visible="showDialog"
      width="400px"
      :before-close="btnCancel"
    >
      <el-form
        label-width="80px"
        class="demo-ruleForm"
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        size="small"
      >
        <el-form-item label="学科名称" prop="subjectName">
          <el-input
            placeholder="请输入学科名称"
            v-model="ruleForm.subjectName"
            style="width: 100%"
          ></el-input>
        </el-form-item>
        <el-form-item label="是否显示" prop="isFrontDisplay">
          <el-switch
            :active-value="1"
            :inactive-value="0"
            v-model="ruleForm.isFrontDisplay"
            active-color="#13ce66"
            inactive-color="#ff4949"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="btnCancel">取 消</el-button>
        <el-button type="primary" @click="btnOK">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// 学科添加
import { add, update, detail } from "@/api/hmmm/subjects";
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      ruleForm: {
        id: null,
        subjectName: "",
        isFrontDisplay: 1,
      },
      rules: {
        subjectName: [
          { required: true, message: "请输入学科名称", trigger: "blur" },
        ],
        isFrontDisplay: [
          { type: "number", message: "请选择是否显示", tirgger: "blur" },
        ],
      },
    };
  },
  computed: {
    showTitle() {
      return this.ruleForm.id ? "修改学科" : "新增学科";
    },
  },
  methods: {
    // 新增 提交确定
    btnOK() {
      this.showDialog = true; // 显示弹层
      this.$nextTick(() => {
        this.$refs.ruleForm.validate(async (valid) => {
          console.log(valid, "valid");
          // 只有校验通过的情况下 才会执行await的下方内容
          if (valid) {
            // 后续对表单数据的处理
            console.log(this.ruleForm, "ruleForm");
            const data = { ...this.ruleForm };

            if (this.ruleForm.id) {
              // 学科修改
              await update(data).then(() => {
                this.$emit("newDataes", this.ruleForm);
              });
            } else {
              // 学科添加
              console.log(this.ruleForm);
              await add(this.ruleForm).then(() => {
                this.$emit("newDataes", this.ruleForm);
              });
              this.$message.success("操作成功");
            }

            // 关闭弹窗
            // this.$emit("update:showDialog", false);
            this.btnCancel();
          }
        });
      });
    },
    // 弹层显示
    dialogFields() {
      this.showDialog = true;
    },
    // 弹层隐藏
    dialogFields() {
      this.showDialog = false;
    },
    // 退出
    btnCancel() {
      this.ruleForm = {
        subjectName: "",
        isFrontDisplay: 1,
      };
      this.$emit("update:showDialog", false);
      this.$refs.ruleForm.resetFields(); // 重置校验字段
      this.$emit("newDataes");
    },

    xiugai(data) {
      console.log(data);
      this.ruleForm = data;
      console.log(this.ruleForm);
    },
  },
};
</script>

<style  rel="stylesheet/scss" scoped lang='scss'>
.dialog-footer {
  text-align: right;
}
</style>
